/**
 * Project: TacticaPescaditos
 * File: InterceptBall.java
 */
package org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.fsm.state;

import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.GoalKeeper;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.messages.Telegram;

/**
 * @author Carlos Alberto
 * class: InterceptBall
 */
public class InterceptBall implements State<GoalKeeper> {
	
	/**
	 * 
	 */
	private InterceptBall() {
	}

	public static final State<GoalKeeper> instance = new InterceptBall();

	public void enter(GoalKeeper keeper) {
		keeper.getSteering().pursuitOn();
	}

	public void execute(GoalKeeper keeper) {
		
		if (keeper.getTactica().isKickGoal() ){
			keeper.getSteering().setTarget(keeper.getBall().getPosition());
			keeper.getSteering().pursuitOff();
			keeper.getSteering().seekOn();
		}
		
		  if (keeper.getTactica().isControlling() || (keeper.isFarFromGoal() && !keeper.isClosestToBall() )  ) {
			  keeper.getStateMachine().changeState(ReturnToHome.instance);
		    return;
		  }

		  
		  if (keeper.canKick()) {
			  keeper.getStateMachine().changeState(KickOff.instance);
		  }
		
	}

	public void exit(GoalKeeper keeper) {
		keeper.getSteering().pursuitOff();
		 keeper.getSteering().seekOff();
		
	}

	public boolean onMessage(GoalKeeper keeper, Telegram telegram) {
		return false;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "InterceptBall";
	}



}
